package com.keyinong.jiashiyuan.adapter;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.jiashiyuan.R;
import com.keyinong.jiashiyuan.entity.DingdanEntity;
import com.keyinong.jiashiyuan.tools.HttpTools;
import com.keyinong.jiashiyuan.view.BaiduActivity;
import com.keyinong.jiashiyuan.view.DetaileActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class DingdanAdapter extends BaseAdapter{
	Context context;
	ArrayList<DingdanEntity> dingdansList;
	HttpTools httpTools=null;

	public DingdanAdapter(Context context, ArrayList<DingdanEntity> dingdansList) {
		super();
		this.context = context;
		this.dingdansList = dingdansList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dingdansList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dingdansList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHandle handle;
		if(view==null){
			handle=new ViewHandle();
			view=LayoutInflater.from(context).inflate(R.layout.item_dingdan, null);
			handle.tv_shangjia=(TextView)view.findViewById(R.id.tv_shangjia);
			handle.tv_dizhi=(TextView)view.findViewById(R.id.tv_dizhi);
			handle.tv_time=(TextView)view.findViewById(R.id.tv_time);
			handle.tv_earlytime=(TextView)view.findViewById(R.id.tv_earlytime);
			handle.bt_xiangqing=(Button)view.findViewById(R.id.bt_xiangqing);
			handle.bt_phone=(Button)view.findViewById(R.id.bt_phone);
			handle.bt_daohang=(Button)view.findViewById(R.id.bt_daohang);
			handle.bt_queren=(Button)view.findViewById(R.id.bt_queren);
			handle.bt_cexiao=(Button)view.findViewById(R.id.bt_cexiao);
			view.setTag(handle);
		}else {
			handle = (ViewHandle) view.getTag();
		}
		String state = dingdansList.get(position).getState();
		if(state.equals("3")){
			handle.bt_queren.setVisibility(View.VISIBLE);
			handle.bt_cexiao.setVisibility(View.INVISIBLE);
		}
		if(state.equals("4")){
			handle.bt_queren.setVisibility(View.INVISIBLE);
			handle.bt_cexiao.setVisibility(View.VISIBLE);
		}
		handle.tv_shangjia.setText("   "+dingdansList.get(position).getShopname());
		handle.bt_phone.setText(dingdansList.get(position).getCellphone());
		handle.tv_dizhi.setText(dingdansList.get(position).getAddress());
		handle.tv_time.setText("-"+dingdansList.get(position).getLate_time());
		handle.tv_earlytime.setText(dingdansList.get(position).getEarly_time());
//		SharedPreferences sp = context.getSharedPreferences("spReg",Context.MODE_PRIVATE);
//		Editor editor = sp.edit();
//		editor.putString("mudidi", mudidi);
//		editor.commit();
		handle.bt_xiangqing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, DetaileActivity.class);
				intent.putExtra("id", dingdansList.get(position).getId());
				context.startActivity(intent);
			}
		});
		handle.bt_daohang.setOnClickListener(new OnClickListener() {

//			SharedPreferences sp = context.getSharedPreferences("spReg",Context.MODE_PRIVATE);
//			String Latitude = sp.getString("Latitude", "");
//			String Longitude = sp.getString("Longitude", "");
//			String mudidi = sp.getString("handle.tv_dizhi.getText().toString()", "");
//
//			private String origin= Latitude+","+Longitude;
//			private String origin_region;
//			private String destination_region;
//			private String coord_type;
//			private String zoom;
//			private String src;
//			private String destination=mudidi;
//			private String mode="driving";
//			private String region;

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("lng", dingdansList.get(position).getLng());
				intent.putExtra("lat", dingdansList.get(position).getLat());
//				Log.i("info", dingdansList.get(position).getLng());
//				Log.i("info", dingdansList.get(position).getLat());
				intent.setClass(context, BaiduActivity.class);
				context.startActivity(intent);		
//				Api api =new Api();
//				api.goToNaviActivity(context, origin, destination, mode, region, origin_region, destination_region, coord_type, zoom, src);
				
			}
		});
		handle.bt_queren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				dingdansList = new ArrayList<DingdanEntity>();
				SharedPreferences sp=context.getSharedPreferences("spReg", Context.MODE_PRIVATE);
				String token = sp.getString("token", "");
				String orderid = dingdansList.get(position).getId();
				String state = dingdansList.get(position).getState();
//				Log.i("info", "orderid"+orderid);
//				Log.i("info", "state"+state);
//				Log.i("info", token);
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.Refresh(token, "4",orderid,new JsonHttpResponseHandler(){
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
//							Log.i("zq",response.toString());
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if(code==true){
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								handle.bt_cexiao.setVisibility(View.VISIBLE);
								handle.bt_queren.setVisibility(View.INVISIBLE);
							}else{
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		handle.bt_cexiao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences sp=context.getSharedPreferences("spReg", Context.MODE_PRIVATE);
				String token = sp.getString("token", "");
				String id = dingdansList.get(position).getId();
				String state = dingdansList.get(position).getState();
//				Log.i("info", "state"+state);
//				Log.i("info", token);
					if (httpTools == null) {
						httpTools = new HttpTools(context);
					}
					httpTools.Refresh(token, "3",id,new JsonHttpResponseHandler(){
						@Override
						public void onSuccess(int statusCode, Header[] headers,
								JSONObject response) {
							// TODO Auto-generated method stub
							super.onSuccess(statusCode, headers, response);
							try {
//								Log.i("zq",response.toString());
								boolean code = response.getBoolean("code");
								String msg = response.getString("msg");
								if (code == true) {
									Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
									handle.bt_cexiao.setVisibility(View.INVISIBLE);
									handle.bt_queren.setVisibility(View.VISIBLE);
								} else {
									Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
					
//				Toast.makeText(context, "已撤销", Toast.LENGTH_SHORT).show();
			}
		});
		handle.bt_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				  Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + handle.bt_phone.getText().toString()));    
				    context.startActivity(dialIntent);   
			}
		});
		return view;
	}

	class ViewHandle {
		/**商家*/
		TextView tv_shangjia;
		/**订单详情*/
		Button bt_xiangqing;
		/**地址*/
		TextView tv_dizhi;
		/**送达时间*/
		TextView tv_time;
		TextView tv_earlytime;
		/**电话*/
		Button bt_phone;
		/**导航*/
		Button bt_daohang;
		/**确认*/
		Button bt_queren;
		/**撤销*/
		Button bt_cexiao;

	}

}
